// pages/movieIndex/movieIndex.js
const movie = require("../../models/movie")
const movieSearch = require("../../models/movieSearch")
Page({
  data: {
    value:[],
    searchResult:[]
  },
  onLoad:async function (options) {
    var inTheaters = await movie("inTheaters");
    var comingSoon = await movie("comingSoon");
    var top250 = await movie("top250");
    this.setData({
      value:[
        {
          title:"正在热映",
          class:"inTheaters",
          res:inTheaters.data.res.slice(0,3)
        },
        {
          title:"即将上映",
          class:"comingSoon",
          res:comingSoon.data.res.slice(0,3)
        },
        {
          title:"Top250",
          class:"top250",
          res:top250.data.res.slice(0,3)
        }
      ]
    })
    console.log(this.data.value);
  },

  more(event){
    // console.log(event.currentTarget.dataset.id);
    let {id,title} = event.currentTarget.dataset
    wx.navigateTo({
      url: `../movieMore/movieMore?id=${id}&title=${title}`
    })
  },

  // 搜索功能
  async search(event){
    // console.log(event.detail);
    if(event.detail.trim()!=""){
      var searchResult = await movieSearch(event.detail);
      if(searchResult.data.result){
        this.setData({
          searchResult:searchResult.data.result
        })
      }else{
        this.setData({
          searchResult:["无结果"]
        })
      }
      console.log(this.data.searchResult);
    }else{
      this.setData({
        searchResult:[]
      })
    }

  }
})